package com.iotp.service.knowledge_type;

import com.iotp.dao.knowledge_type.KnowledgeTypeMapper;
import com.iotp.entity.base.CodeEnum;
import com.iotp.entity.base.ResponseResult;
import com.iotp.entity.base.TreeNode;
import com.iotp.entity.knowledge_type.KnowledgeType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.dsig.keyinfo.KeyName;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 知识管理
 */
@Service("knowledgeTypeService")
public class KnowledgeTypeServiceImpl implements KnowledgeTypeService {
    @Autowired
    private KnowledgeTypeMapper knowledgeTypeMapper;//知识管理

    /**
     * 添加根节点
     * @param knowledgeType
     * @return
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED,isolation= Isolation.DEFAULT)
    public ResponseResult addRoot(KnowledgeType knowledgeType) {
        ResponseResult rr = null;
        if(knowledgeType == null){
            rr = new ResponseResult(CodeEnum.参数不合法.code,CodeEnum.参数不合法.msg,null);
        }else{
            HashMap<String, Object> params = new HashMap<>();
            params.put("level",1);
            params.put("type_code","100");
            KnowledgeType select_kt = knowledgeTypeMapper.selectByLength(params);
            if(select_kt == null){
                knowledgeType.setTypeCode("1001");
            }else{
                knowledgeType.setTypeCode((Integer.parseInt(select_kt.getTypeCode())+1)+"");
            }
            knowledgeType.setLevel(1);
            knowledgeType.setParentId(0L);
            knowledgeType.setCreateTime(System.currentTimeMillis());
            knowledgeType.setUpdateTime(System.currentTimeMillis());
            Long id = this.knowledgeTypeMapper.insert(knowledgeType);
            rr = new ResponseResult(CodeEnum.成功.code,CodeEnum.成功.msg,id);
        }
        return rr;
    }


    /**
     * 获取树数据
     * @param type 0：第一个节点展开 1：全部展开 2：全部闭合
     * @return
     */
    @Override
    public ResponseResult getAllNode(int type) {
        List<KnowledgeType> list = this.knowledgeTypeMapper.selectByCondition(new KnowledgeType());
        List<TreeNode> tree = new ArrayList<TreeNode>();
        if(null != list && !list.isEmpty()){
            tree = createTree(type, list);
        }
        return new ResponseResult(CodeEnum.成功.code, CodeEnum.成功.msg, tree);
    }

    /**
     * 构建结构
     * @param type
     * @param list
     * @return
     */
    public static List<TreeNode> createTree(int type, List<KnowledgeType> list){
        List<TreeNode> res = new ArrayList<TreeNode>();
        for(KnowledgeType item : list){
            TreeNode t = new TreeNode();
            t.setId(item.getId());
            t.setPId(item.getParentId());
            t.setName(item.getTypeName());
            if(type == 0){
                if(item.getTypeCode().equals("1001")){
                    t.setOpen(true);
                }else{
                    t.setOpen(false);
                }
            }else if(type == 1){
                if(item.getTypeCode().length() == 4){
                    t.setOpen(true);
                }else{
                    t.setOpen(false);
                }
            }else if(type == 2){
                t.setOpen(false);
            }
            res.add(t);
        }
        return res;
    }

    /**
     * 节点操作
     * @param type              1：编辑   2：删除
     * @param knowledgeType     操作对象
     * @return
     */
    @Override
    public ResponseResult operationNode(int type, KnowledgeType knowledgeType) {
        ResponseResult rr = null;
        switch (type){
            case 1:
                //节点编辑
                if(knowledgeType.getId().longValue() == 0L){
                    KnowledgeType parent = this.knowledgeTypeMapper.selectByPrimaryKey(knowledgeType.getParentId());
                    if(parent != null){
                        //添加
                        HashMap<String, Object> params = new HashMap<>();
                        params.put("level",parent.getLevel()+1);
                        params.put("type_code", parent.getTypeCode());
                        KnowledgeType select_kt = knowledgeTypeMapper.selectByLength(params);
                        if(select_kt == null){
                            knowledgeType.setTypeCode(parent.getTypeCode()+"0001");
                        }else{
                            knowledgeType.setTypeCode((Integer.parseInt(select_kt.getTypeCode())+1)+"");
                        }
                        knowledgeType.setLevel(parent.getLevel()+1);
                        knowledgeType.setParentId(0L);
                        knowledgeType.setCreateTime(System.currentTimeMillis());
                        knowledgeType.setUpdateTime(System.currentTimeMillis());
                        Long id = this.knowledgeTypeMapper.insert(knowledgeType);
                        rr = new ResponseResult(CodeEnum.成功.code, CodeEnum.成功.msg, id);
                    }else{
                        rr = new ResponseResult(CodeEnum.失败.code, CodeEnum.失败.msg, null);
                    }
                }else{
                    //编辑
                    KnowledgeType edit = new KnowledgeType();
                    edit.setId(knowledgeType.getId());
                    edit.setTypeName(knowledgeType.getTypeName());
                    edit.setUpdateTime(System.currentTimeMillis());
                    this.knowledgeTypeMapper.updateByPrimaryKeySelective(edit);
                    rr = new ResponseResult(CodeEnum.成功.code, CodeEnum.成功.msg, null);
                }
                break;
            case 2:
                //节点删除
                if(null == knowledgeType.getId() || knowledgeType.getId().longValue() == 0L){
                    rr = new ResponseResult(CodeEnum.参数不合法.code, CodeEnum.参数不合法.msg, null);
                }else{
                    this.knowledgeTypeMapper.deleteByPrimaryKey(knowledgeType.getId());
                    rr = new ResponseResult(CodeEnum.成功.code, CodeEnum.成功.msg, null);
                }
                break;
            default:
                break;
        }
        return rr;
    }

    @Override
    public List<KnowledgeType> queryList(KnowledgeType knowledgeType) {
        return this.knowledgeTypeMapper.selectByCondition(knowledgeType);
    }

    @Override
    public KnowledgeType queryById(Long id) {
        return this.knowledgeTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(KnowledgeType knowledgeType) {
        this.knowledgeTypeMapper.updateByPrimaryKeySelective(knowledgeType);
    }

    @Override
    public void delete(Long id) {
        this.knowledgeTypeMapper.deleteByPrimaryKey(id);
    }


}
